package com.easymin.weixin.controller.h5.v2;

import com.google.common.base.Preconditions;

import com.alibaba.fastjson.JSONObject;
import com.easymin.weixin.common.api.DaijiaApi;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.manager.ErrandManager;
import com.easymin.weixin.manager.WxApplicationManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by shine on 16/1/4.
 */
@RestController
@RequestMapping("h5/v2/pt")
public class PaotuiV2Controller {

  @Autowired
  private DaijiaApi daijiaApi;

  @Autowired
  private ErrandManager errandManager;

  @Autowired
  private WxApplicationManager wxApplicationManager;


  /**
   * 预算费用.
   * @param appKey 系统key
   * @param startLat 开始纬度
   * @param startLng 开始经度
   * @param endLat 结束纬度
   * @param endLng 结束经度
   * @return Map
   */
  @RequestMapping("calculationPrice")
  public JSONObject calculationPrice(
      @RequestParam("appkey") String appKey,
      @RequestParam("city") String city,
      @RequestParam(value = "ack", required = false) String ack,
      @RequestParam(value = "passengerId", required = false) Long passengerId,
      @RequestParam("startLat") double startLat,
      @RequestParam("startLng") double startLng,
      @RequestParam("endLat") double endLat,
      @RequestParam("endLng") double endLng) {
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
    
    Preconditions.checkArgument(null != wxApplication, "appKey错误");

    return daijiaApi.getErrandPrice(
        wxApplication.getAddress(), passengerId, city, ack, startLat, startLng, endLat, endLng);
  }

  /**
   * 预约订单.
   * @param appKey 系统key
   * @param content 内容
   * @param startAddress 开始地址
   * @param startLat 开始纬度
   * @param startLng 开始经度
   * @param shouldPay 支付金额
   * @param travelTime 预算总时间
   * @param mileage 总公里数
   * @param startPrice 开始价
   * @param mileagePrice 里程价格
   * @param travelTimePrice 时间价格
   * @param deliverAddress 收货地址
   * @param deliverLat 收货纬度
   * @param deliverLng 收货经度
   * @param openId 微信id
   * @return Object
   */
  @RequestMapping(value = "makeAnAppointment", method = RequestMethod.POST)
  public Object makeAnAppointment(
      @RequestParam("title") String title,
      @RequestParam("content") String content,
      @RequestParam("deliverTime") String deliverTime,
      @RequestParam("deliverAddress") String deliverAddress,
      @RequestParam("deliverLat") Double deliverLat,
      @RequestParam("deliverLng") Double deliverLng,
      @RequestParam("phone") String passengerPhone,
      @RequestParam(required = false, value = "openId") String openId,
      @RequestParam(required = false, value = "passengerId") Long passengerId,
      @RequestParam(required = false, value = "shouldPay") Double shouldPay,
      @RequestParam(required = false, value = "startTime") String startTime,
      @RequestParam(required = false, value = "startPhone") String startPhone,
      @RequestParam(required = false, value = "endPhone") String endPhone,
      @RequestParam(required = false, value = "startAddress") String startAddress,
      @RequestParam(required = false, value = "startLat") Double startLat,
      @RequestParam(required = false, value = "startLng") Double startLng,
      @RequestParam(required = false, value = "orderType") String orderType,
      @RequestParam(required = false, value = "travelTime") Integer travelTime,
      @RequestParam(required = false, value = "mileage") Double mileage,
      @RequestParam(required = false, value = "startPrice") Double startPrice,
      @RequestParam(required = false, value = "mileagePrice") Double mileagePrice,
      @RequestParam(required = false, value = "travelTimePrice") Double travelTimePrice,
      @RequestParam("appkey") String appKey) throws Exception {

    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
    Preconditions.checkArgument(null != wxApplication, "appKey错误");

    String channelName = "网站预约";

    if (org.apache.commons.lang3.StringUtils.isNotBlank(openId)) {
      channelName = "微信预约";
    }

    return errandManager.createOrder(
        title, content, deliverTime, deliverAddress, deliverLat, deliverLng, 
        null, shouldPay, passengerId, passengerPhone, channelName, null, null, null, 
        startTime, startPhone, endPhone, startAddress, startLat, startLng, orderType, 
        startPrice, mileagePrice, travelTimePrice, travelTime, mileage, appKey);

  }
}
